package cn.codexin.wechatofficial.service;

import cn.codexin.wechatofficial.dto.UserInfoAccessTokenDTO;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.UnapprovedUserMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.UserMapper;
import cn.codexin.wechatofficial.model.dbo.UnapprovedUserExample;
import cn.codexin.wechatofficial.model.dbo.User;
import cn.codexin.wechatofficial.model.dbo.UserExample;
import cn.codexin.wechatofficial.wechat.WeChatProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserInfoService {

    @Autowired
    private WeChatProvider weChatProvider;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UnapprovedUserMapper unapprovedUserMapper;


    public String getUserOpenId(String code) {
//        获取openid
        UserInfoAccessTokenDTO userInfoAccessToken = weChatProvider.getUserInfoAccessToken(code);
        return userInfoAccessToken.getOpenid();
    }

    public Boolean isChecked(String openid) {
        if(openid!=null){
            UserExample example=new UserExample();
            example.createCriteria().andOpenidEqualTo(openid);
            List<User> users = userMapper.selectByExample(example);
            if (users.size() != 0) {
                return users.get(0).getIsChecked();
            }
        }
        return false;
    }

    public Boolean isRegister(String openid) {
        if(openid != null){
            UserExample example = new UserExample();
            example.createCriteria().andOpenidEqualTo(openid);
            long count1 = userMapper.countByExample(example);
            UnapprovedUserExample unapprovedUserExample = new UnapprovedUserExample();
            unapprovedUserExample.createCriteria().andOpenidEqualTo(openid);
            long count2 = unapprovedUserMapper.countByExample(unapprovedUserExample);
            return count1 + count2 > 0;
        }
        return false;
    }

    public Boolean isAdmin(String openid) {
        UserExample example=new UserExample();
        example.createCriteria().andOpenidEqualTo(openid);
        List<User> users = userMapper.selectByExample(example);
        return users.get(0).getType();
    }

    public String getPath(String servletPath, Map<String, String[]> parameterMap) {
        if(parameterMap.isEmpty()){
            return servletPath;
        }else {
            final String[] path = {servletPath + "?"};
            parameterMap.forEach((k,v)->{
                path[0] = path[0] +k+'='+v[0]+'&';
            });
            //substring不包括endIndex(可看源码分析)
            return path[0].substring(0, path[0].length() - 1);
        }
    }
//    //snsapi_base方式(不修改user_info_access_token和user_info表)
//    public UserInfo getUserInfoByCodeBase(String code) {
//        //获取openid
//        UserInfoAccessTokenDTO userInfoAccessToken = weChatProvider.getUserInfoAccessToken(code);
//        UserInfoExample example = new UserInfoExample();
//        //根据openid从user_info表中查询数据
//        example.createCriteria().andOpenIdEqualTo(userInfoAccessToken.getOpenid());
//        List<UserInfo> userInfos = userInfoMapper.selectByExample(example);
//        if (userInfos.size() != 0) {
//            return userInfos.get(0);
//        } else {
//            UserInfo userInfo = new UserInfo();
//            userInfo.setOpenId(userInfoAccessToken.getOpenid());
//            return userInfo;
//        }
//    }
//
//    //snsapi_userinfo方式(对user_info_access_token和user_info表更新或插入)
//    public UserInfo getUserInfoByCode(String code) {
//        UserInfoAccessTokenDTO userInfoAccessToken = weChatProvider.getUserInfoAccessToken(code);
//        UserInfoAccessToken userToken = new UserInfoAccessToken();
//        userToken.setOpenId(userInfoAccessToken.getOpenid());
//        userToken.setAccessToken(userInfoAccessToken.getAccess_token());
//        userToken.setScope(userInfoAccessToken.getScope());
//        userToken.setRefreshToken(userInfoAccessToken.getRefresh_token());
//        userToken.setGmtTokenExpire(userInfoAccessToken.getExpires_in() * 1000 - 300 * 1000 + System.currentTimeMillis());
//        userToken.setGmtRefreshTokenExpire(30L * 24L * 3600L * 1000L - 300L * 1000L + System.currentTimeMillis());
//        //修改user_info_access_token表
//        UserInfoAccessTokenExample tokenExample = new UserInfoAccessTokenExample();
//        tokenExample.createCriteria().andOpenIdEqualTo(userToken.getOpenId());
//        List<UserInfoAccessToken> userInfoAccessTokens = userInfoAccessTokenMapper.selectByExample(tokenExample);
//        if (userInfoAccessTokens.size() != 0) {
//            userInfoAccessTokenMapper.updateByExampleSelective(userToken, tokenExample);
//        } else {
//            userInfoAccessTokenMapper.insert(userToken);
//        }
//
//        UserInfoDTO userInfo = weChatProvider.getUserInfo(userToken.getOpenId(), userToken.getAccessToken());
//        UserInfo user = new UserInfo();
//        user.setOpenId(userInfo.getOpenid());
//        BeanUtils.copyProperties(userInfo, user);
//        //修改user_info表
//        UserInfoExample userInfoExample = new UserInfoExample();
//        userInfoExample.createCriteria().andOpenIdEqualTo(userToken.getOpenId());
//        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
//        if (userInfos.size() != 0) {
//            userInfoMapper.updateByExampleSelective(user, userInfoExample);
//        } else {
//            userInfoMapper.insert(user);
//        }
//
//        return user;
//    }
}
